﻿using System;
using Database;
using System.Collections.Generic;
using System.Data;

namespace ObjectManager
{
    class BoMonDAO : IDatabase
    {
        private ConnectionDB cnn = null;

        public BoMonDTO BoMonDTO { get; set; }

        public BoMonDAO()
        {
            cnn = Connection.Connect();
            BoMonDTO = new BoMonDTO();
        }

        public int Insert()
        {
            if (!string.IsNullOrEmpty(BoMonDTO.Ten))
            {
                if (!string.IsNullOrEmpty(BoMonDTO.KyHieu))
                {
                    return cnn.ExecuteProcedure("InsertBoMon", BoMonDTO.KyHieu, BoMonDTO.Ten);
                }
                return cnn.ExecuteProcedure("InsertBoMonTen", BoMonDTO.Ten);
            }
            return -1;
        }

        public int Update()
        {
            if (!string.IsNullOrEmpty(BoMonDTO.Ten))
            {
                return cnn.ExecuteProcedure("UpdateBoMon", BoMonDTO.Ma, BoMonDTO.KyHieu, BoMonDTO.Ten);
            }
            return -1;
        }

        public int Delete()
        {
            if (!string.IsNullOrEmpty(BoMonDTO.Ten))
            {
                return cnn.ExecuteProcedure("DeleteBoMon_Ten", BoMonDTO.Ten);
            }
            return -1;
        }

        public List<string> ListName()
        {
            object[][] temp = cnn.Querys("select distinct Ten from BoMon;");
            List<string> result = new List<string>();
            for (int i = 0; i < temp.Length; i++)
            {
                result.Add(temp[i][0].ToString());
            }
            return result;
        }

        public DataTable ListAll()
        {
            return cnn.QueryProcedure("DanhSachBoMon");
        }

        public DataTable ListAll(int Ma)
        {
            return cnn.QueryProcedure("DanhSachBoMon_Tim_Ma", Ma);
        }

        public DataTable ListAll(string Ten, string KyHieuBM)
        {
            return cnn.QueryProcedure("DanhSachBoMon_Tim", KyHieuBM, Ten);
        }

        public DataTable Ma()
        {
            return cnn.QueryProcedure("MaBoMon");
        }
    }
}
